package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectPageParamDto;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.ProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private ProjectPlanMapper projectPlanMapper;

    //分页查询
    @Override
    public PageResponse<NursingProjectVo> selectByPage(NursingProjectPageParamDto nursingProjectPageParamDto) {
        //开启分页插件
        PageHelper.startPage(nursingProjectPageParamDto.getPageNum(), nursingProjectPageParamDto.getPageSize());
        //调用持久层
        Page<List<NursingProjectVo>> result = nursingProjectMapper.selectByPage(nursingProjectPageParamDto);
        //返沪结果（方法已封装）
        return PageResponse.of(result, NursingProjectVo.class);
    }

    //新增项目
    @Override
    public void addProject(NursingProjectDto nursingProjectDto) {
        //新增的项目名称不能和已有的重复
        Long count = nursingProjectMapper.selectByName(nursingProjectDto.getName());
        if (count > 0) {
            throw new BaseException(BasicEnum.PROJECT_INSERT_FAIL_REPEAT_NAME);
        }
        //新增项目（统一使用与表字段相同的实体类映射【其父类BaseEntity封装了表的公共字段】）
        NursingProject nursingProject = new NursingProject();
        nursingProject.setStatus(1); //为状态赋初始值
        BeanUtils.copyProperties(nursingProjectDto, nursingProject); //SpringBoot工具类API：将接受的参数copy到实体类
        nursingProjectMapper.insertProject(nursingProject);
    }

    //根据ID查询项目
    @Override
    public NursingProjectVo getById(Integer id) {
        NursingProjectVo nursingProjectVo = nursingProjectMapper.selectById(id);
        return nursingProjectVo;
    }
    //更新项目信息
    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        //不可更新已与护理计划绑定的项目信息
        Long count = projectPlanMapper.selectByProjectId(nursingProjectDto.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.PROJECT_UPDATE_FAIL_PLAN_EXIT);
        }
        //若更新名称，不可使用已存在名称
        Long nameCount = nursingProjectMapper.selectOthersByName(nursingProjectDto.getName());
        if (nameCount > 0) {
            throw new BaseException(BasicEnum.PLAN_INSERT_FAIL_REPEAT_NAME);
        }
        //更新项目信息
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);
        nursingProjectMapper.update(nursingProject);
    }

    //更新项目状态
    @Override
    public void updateStatus(Long id, Integer status) {
        //不可更新已与护理计划绑定的项目信息
        Long count = projectPlanMapper.selectByProjectId(id);
        if (count > 0) {
            throw new BaseException(BasicEnum.PROJECT_UPDATE_FAIL_PLAN_EXIT);
        }
        //更新项目的状态
        nursingProjectMapper.updateProjectStatus(id,status);
    }

    //删除项目
    @Override
    public void deleteProjectById(Long id) {
        //已经纳入计划的项目无法删除
        Long count = projectPlanMapper.selectByProjectId(id);
        if (count > 0) {
            throw new BaseException(BasicEnum.PROJECT_DELETE_FAIL_PLAN_EXIT);
        }
        //删除项目
        nursingProjectMapper.delete(id);
    }

    //查询所有项目
    @Override
    public List<NursingProjectVo> getAll() {
        List<NursingProjectVo> nursingProjectVo = nursingProjectMapper.select();
        return nursingProjectVo;
    }


}
